package com.yuyu.excel.config;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import com.yuyu.excel.utils.TenantContextHolder;
import net.sf.jsqlparser.expression.StringValue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MybatisPlusConfig {

    /**
     * MybatisPlus插件
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
//        mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        TenantLineInnerInterceptor tenantLineInnerInterceptor = new TenantLineInnerInterceptor();
        tenantLineInnerInterceptor.setTenantLineHandler(() -> {
            String tenantId = TenantContextHolder.getTenantId();
            if (StrUtil.isBlankIfStr(tenantId)) {
                tenantId = "001";
            }
            return new StringValue(tenantId);
        });
        mybatisPlusInterceptor.addInnerInterceptor(tenantLineInnerInterceptor);
        return mybatisPlusInterceptor;
    }
}
